package com.vc.core;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;

import android.content.Context;
import android.graphics.PixelFormat;
import android.hardware.Camera;
import android.hardware.Camera.PreviewCallback;
import android.hardware.Camera.Size;
import android.os.Build;
import android.util.Log;
import android.view.Display;
import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.WindowManager;

public class VcCamera implements PreviewCallback {
	
	private static final String TAG = "VcCamera";
	public final int FRONT_CAMERA = 1;
	public final int BACK_CAMERA = 2;
	
	private Camera camera = null;
	
	private Context context = null;

	private VideoController mVideoCtrl;
	
	private static boolean isVcOpened = false;
	//private static boolean isCameraOpened = false;

	int SDK_VERSION;	
	String DEV_MODEL;
	String DEV_MANUFACTURER;
	private Display devDisplay;

	public int CUR_CAMERA = 0;
	public int NUM_CAMERA = 0;

	private int CameraId;
	
	private int mFrontCameraAngle = 0;
	private int mBackCameraAngle = 0;	
	//private int CompenSateRecvAngle = 0;
	//private int CompenSateSendAngle = 0;
	private static CameraInformation Info = new CameraInformation();
		
	/**
	 * 构造器，将控制器传进来
	 * @param qVc
	 */
	public VcCamera(VideoController vc) {		  
		  this.mVideoCtrl = vc;		  
		  this.context = this.mVideoCtrl.getAppContext();		  
		  this.devDisplay = ((WindowManager)this.context.getSystemService("window")).getDefaultDisplay();		  
		  this.SDK_VERSION = Build.VERSION.SDK_INT;
		  this.DEV_MODEL = Build.MODEL;
		  this.DEV_MANUFACTURER = Build.MANUFACTURER;		  
		  Log.d(TAG, "DEV_MANUFACTURER="+this.DEV_MANUFACTURER+",DEV_MODEL="+this.DEV_MODEL+",SDK_VERSION="+SDK_VERSION);
	}
	
	
	/**
	 * 开启摄像头
	 * @param holder
	 * @return
	 */
	public synchronized boolean startCamera(SurfaceHolder holder){
	    Log.d(TAG, "startCamera begin.");
	    
	    if(isVcOpened){
	    	 Log.d(TAG, "Camera isCameraOpened!");
		     return false;
	    }
	    
	    if(this.camera == null){
	    	openCamera();
	    }
	    
	    if ((this.camera == null) && (!isVcOpened)) {
	      Log.d(TAG, "Camera not open.");
	      return false;
	    }
	    
	    setCameraPara(this.devDisplay.getWidth(), this.devDisplay.getHeight());
	    
	    try
	    {
	      if (!this.mVideoCtrl.mPauseVideo) {
	    	camera.setPreviewCallback(this);
	    	camera.setPreviewDisplay(holder);
	        camera.startPreview();
	        isVcOpened = true;
	      }
	    } catch (Exception e) {
	      Log.d(TAG, "startCamera error.");
	      return false;
	    }

	    Log.d(TAG, "startCamera end.");

	    return true;
	}
	
	/**
	 * 关闭摄像头
	 * @return
	 */
	 public synchronized boolean closeCamera() {
		 
		 Log.d("VcCamera", "closeCamera begin.");

		 if ((camera == null) && (!isVcOpened)) {
		      Log.d(TAG, "Camera not open.");
		 }
		 
		 if (camera != null) {
			 if (isVcOpened) {
		        camera.setPreviewCallback(null);
		        camera.stopPreview();
		        camera.release();
		     }
		     this.camera = null;
		 }
		 
		 isVcOpened = false;
		 Log.d(TAG, "closeCamera end.");
		 return true;
	 }
	 
	
	@Override
	public void onPreviewFrame(byte[] data, Camera camera) {
		Log.d(TAG, "onPreviewFrame videoChatSettings="+VideoChatSettings.getVal());
		long account = 888888;
		if(mVideoCtrl!=null){		
			mVideoCtrl.sendData(
					data, 
					account, 
					VideoChatSettings.getWidth(),
					VideoChatSettings.getHeight()
					);
		}
	}
	
	/**
	 * 设置旋转角度
	 * @param camera
	 * @param degree
	 */
	private void setDisplayOrientation(Camera camera, int degree) {
		try {
			Method m = camera.getClass().getMethod("setDisplayOrientation", new Class[] { int.class });
			m.invoke(camera, new Object[] { degree });
		} catch (Exception e) {
			Log.d(TAG, "setDisplayOrientation", e);
		}
	}
	
	/**
	 * 开启摄像
	 * @return
	 */
	private synchronized boolean openCamera() {
		this.CUR_CAMERA = 0;		
		if (this.context == null) {
		   Log.e(TAG, "context is null");
		   return false;
		}
		if (getFrontCamera() || getBackCamera()) {
			return true;
		}else{
			Log.e(TAG, "openCamera failed");
		}
		return false;
	}
	
	
	/**
	 * 设置视频输出参数
	 * @param w
	 * @param h
	 * @throws RuntimeException
	 */
	private void setCameraPara(int w, int h) throws RuntimeException {		
		synchronized (Info) {
			
			//setDisplayOrientation(camera,90);//测试用
			
			if (fitSdkVersion()) {
				setCameraDisplayOrientation(CameraId, camera);
			} else {
				adjustDirection(camera);
			}
		}
		Camera.Parameters parameters = camera.getParameters();
		getSupportPreviewFormat(parameters);
		getSupportPreviewSizes(parameters);
		getSupportFrameRate(parameters);
		//设置预览大小
		if (DEV_MANUFACTURER.equalsIgnoreCase("samsung") && DEV_MODEL.equalsIgnoreCase("GT-I9003")) {
			parameters.setPreviewSize(320, 240);
		} else {
			parameters.setPreviewSize(VideoChatSettings.getWidth(), VideoChatSettings.getHeight());
		}
		//设置预览数据格式
		if (VideoChatSettings.getFormat() != PixelFormat.UNKNOWN) {
			Log.d(TAG, "supportFormat = " + VideoChatSettings.getFormat());
			parameters.setPreviewFormat(VideoChatSettings.getFormat());
		} else {
			Log.d(TAG, "supportFormat = 17(default value)");
			parameters.setPreviewFormat(17);
		}
		//设置预览帧数/s
		parameters.setPreviewFrameRate(VideoChatSettings.getnInFPS());
		try {
			camera.setParameters(parameters);
		} catch (Exception e) {
			Log.e(TAG, "setParameters exception",e);
		}
	}
	
	
	private void adjustDirection(Camera c) {
		try {
			Method m = c.getClass().getMethod("setDisplayOrientation", new Class[] { int.class });
			int angle = 0;
            if (CUR_CAMERA == FRONT_CAMERA) {
            	angle = 90;
                //angle = getPreviewAngleForFrontCamera() % 360;
            } else { // back-facing
            	angle = 0;
                //angle = getPreviewAngleForBackCamera() % 360;
            }
			m.invoke(c, new Object[] {angle});
		} catch (Exception e) {
			Log.d(TAG, "adjustDirection", e);
		}
	}
	
	
	private boolean setCameraDisplayOrientation(int cameraId, Camera camera) {
		CameraInformation Info = getCameraDisplayOrientation(cameraId, camera);

		int result = 0;
		
		if (Info.facing == 1) {
			result = (getOrientation() + getRotation()) % 360;
			result = (360 - result) % 360; // compensate the mirror
		} else { // back-facing
			result = (getOrientation() - getRotation() + 360) % 360;
		}
		/*
		if (Info.facing == 1) {
            result = (result + getPreviewAngleForFrontCamera()) % 360;
        } else { // back-facing
            result = (result + getPreviewAngleForBackCamera()) % 360;
        }*/
		
		setDisplayOrientation(camera, result);

		return true;
	}
	
	/**
	 * 获取当前方向
	 * @return
	 */
	public int getOrientation() {
		if (Info.orientation == -1) {
			if (CUR_CAMERA == FRONT_CAMERA) {
				return 270;
			} else if (CUR_CAMERA == BACK_CAMERA) {
				return 90;
			}
		}
		return Info.orientation;
	}
	
	/**
	 * 设置角度
	 * @param rotation
	 */
	public void setRotation(int rotation) {
		//Info.rotation = (rotation + CompenSateSendAngle) % 360;//(rotation+270)%360;
		Info.rotation = rotation % 360;
	}

	/**
	 * 获取角度
	 * @return
	 */
	public int getRotation() {
		if (Info.rotation == -1) {
			return 0;
		}
		return Info.rotation;
	}
	
	
	private CameraInformation getCameraDisplayOrientation(int cameraId, Camera camera) {
		try {
			Class<?> cameraClass = Class.forName("android.hardware.Camera");
			Object cameraInfo = null;
			Field field_facing = null;
			Field field_orientation = null;

			Class<?> cameraInfoClass = Class.forName("android.hardware.Camera$CameraInfo");
			if (cameraInfoClass == null) {
				Info.rotation = -1;
				Info.orientation = -1;
				return Info;
			}

			cameraInfo = cameraInfoClass.newInstance();
			if (cameraInfo == null) {
				Info.rotation = -1;
				Info.orientation = -1;
				return Info;
			}

			field_facing = cameraInfo.getClass().getField("facing");
			field_orientation = cameraInfo.getClass().getField("orientation");

			if (field_facing == null || field_orientation == null) {
				Info.rotation = -1;
				Info.orientation = -1;
				return Info;
			}

			Method getCameraInfo = cameraClass.getMethod("getCameraInfo",
					int.class, cameraInfoClass);

			if (getCameraInfo == null) {
				Info.rotation = -1;
				Info.orientation = -1;
				return Info;
			}

			getCameraInfo.invoke(null, cameraId, cameraInfo);
			Info.facing = field_facing.getInt(cameraInfo);
			Info.orientation = field_orientation.getInt(cameraInfo);
			
			if (devDisplay == null) {
				Info.rotation = -1;
				return Info;
			}

			Method getRotation = devDisplay.getClass().getMethod("getRotation");

			if (getRotation == null) {
				Info.rotation = -1;
				return Info;
			}

			Object rotation = getRotation.invoke(devDisplay, (Object[]) null);

			switch (Integer.parseInt(rotation.toString())) {
				case Surface.ROTATION_0:
					Info.rotation = 0;
					break;
				case Surface.ROTATION_90:
					Info.rotation = 90;
					break;
				case Surface.ROTATION_180:
					Info.rotation = 180;
					break;
				case Surface.ROTATION_270:
					Info.rotation = 270;
					break;
			}

			return Info;
		} catch (Exception e) {
			Info.rotation = 0;
			//Info.orientation = 270;
			return Info;
		}
	}
	
	/**
	 * 获取支持的预览格式，利用反射是因为android版本兼容性
	 * @param parameters
	 * @return
	 */
	private void getSupportPreviewFormat(Camera.Parameters parameters){
		
		int supportFormat = PixelFormat.UNKNOWN;
		
		if(parameters==null){
			return;
		}
		
		Method getSupportedPreviewFormats = null;
		
		try {
			getSupportedPreviewFormats = parameters.getClass().getMethod("getSupportedPreviewFormats", new Class[]{} );
			@SuppressWarnings("unchecked")
			List<Integer> formats = (List<Integer>) getSupportedPreviewFormats.invoke(parameters, (Object[]) null);
			if(formats != null) {
				StringBuffer sb_format = new StringBuffer();
			    for (int i = 0; i < formats.size(); i++) {
			    	sb_format.append("[").append(formats.get(i)).append("]");	               
	            }
			    Log.d(TAG, "supported preview format: " + sb_format.toString());
			    /*
			     * enum FrameBuffer_Type
				{
					def_DataType_YUV420P	= 0,
					def_DataType_YUV420SP	= 1,
					def_DataType_YV12		= 2,
					def_DataType_RGB565		= 7,
					def_DataType_RGB8888	= 9,
					def_DataType_YUV422SP	= 16
				};
				*/
			    if(formats.contains(842094169))//YV12
				{
					supportFormat = 2;
				}
			    else if(formats.contains(PixelFormat.RGB_565))//ImageFormat.RGB_565
			    {
			    	supportFormat = 7;
				}
			    else if (formats.contains(PixelFormat.RGBX_8888))
				{
					supportFormat = 9;
				}
			    else if(formats.contains(PixelFormat.YCbCr_420_SP))//YCbCr_420_SP(deprecated). ImageFormat.NV21
				{
			    	supportFormat = 1;
				}
			    else if(formats.contains(PixelFormat.YCbCr_422_SP))//YCbCr_422_SP(deprecated). ImageFormat.NV16
				{
					supportFormat = 16;
				}
			    
			    /*
			    if (formats.contains(17))
				{
					supportFormat = 17;
				}
				else if (formats.contains(16)) //YCbCr_422_SP(deprecated). ImageFormat.NV16
				{
					supportFormat = 16;
				}
				else if (formats.contains(20)) // YCbCr_422_I(deprecated), ImageFormat.YUY2  YUYV
				{
					supportFormat = 20;
				}
				else if (formats.contains(842094169))  // YV12
				{
					supportFormat = 842094169;
				}			 
				else if (formats.contains(4))//ImageFormat.RGB_565
				{
					supportFormat = 4;
				}
				else if (formats.contains(PixelFormat.YCbCr_420_SP))
				{
					supportFormat = PixelFormat.YCbCr_420_SP;
				}
				else if (formats.contains(PixelFormat.YCbCr_422_SP))
				{
					supportFormat = PixelFormat.YCbCr_422_SP;
				}
				else if (formats.contains(PixelFormat.RGB_888))
				{
					supportFormat = PixelFormat.RGB_888;
				}
				else if (formats.contains(PixelFormat.RGBX_8888))
				{
					supportFormat = PixelFormat.RGBX_8888;
				}
				else if (formats.contains(PixelFormat.RGB_565))
				{
					supportFormat = PixelFormat.RGB_565;
				} 
				// 后面几种实际上不会运行到，先留着
				else if (formats.contains(100)) // YUV420. custom. To Be Implement
				{
					supportFormat = 100;
				}
				else if (formats.contains(101)) // YVYU. custom. To Be Implement
				{
					supportFormat = 101;
				}
				else if (formats.contains(102)) // UYVY. custom. To Be Implement
				{
					supportFormat = 102;
				}
				else if (formats.contains(103)) // VYUY. custom. To Be Implement
				{
					supportFormat = 103;
				}
				else if (formats.contains(104)) // NV12. custom. To Be Implement
				{
					supportFormat = 104;
				}
				*/
			    
			}
		} catch (Exception e) {
			
		}		
		// 强制修正魅族M9和中兴U880的视频格式
		if (DEV_MANUFACTURER.equalsIgnoreCase("meizu") && DEV_MODEL.equalsIgnoreCase("meizu_m9")) {
			supportFormat = 18;
		} else if (DEV_MANUFACTURER.equalsIgnoreCase("ZTE") && DEV_MODEL.equalsIgnoreCase("ZTE-T U880")) {
			supportFormat = 100;
		}
		VideoChatSettings.setFormat(supportFormat);
	}


	
	/**
	 * 获取FPS，帧数/s，视硬件而定，此处最低保证10帧
	 * @return
	 */
	private void getSupportFrameRate(Camera.Parameters parameters) {
		int nInFPS = 0;
		if(parameters==null){
			return;
		}
	    try{
	        Method getSupportedPreviewFrameRates = parameters.getClass()
	          .getMethod("getSupportedPreviewFrameRates", new Class[0]);
	        if (getSupportedPreviewFrameRates != null)
	        {
	          List<Integer> frameRates = (List<Integer>)getSupportedPreviewFrameRates.invoke(parameters, null);
	          if (frameRates != null)
	          {
	            nInFPS = 0;
	            for (Integer rate : frameRates) {
	              if (rate.intValue() > 9) {
	                int lastValidnInFPS = rate.intValue();

	                if ((nInFPS > lastValidnInFPS) || (nInFPS == 0))
	                  nInFPS = lastValidnInFPS;
	              }
	            }
	          }
	        }
	    }catch (Exception localException2){}
	    
	    if (nInFPS == 0) {
	        nInFPS = 10;
	    }
	    VideoChatSettings.setnInFPS(nInFPS);
	}
	
	/**
	 * 获取支持的最佳的预览屏幕大小
	 * @param parameters
	 */
	private void getSupportPreviewSizes(Camera.Parameters parameters){
		if(parameters==null){
			return;
		}
		Method getSupportedPreviewSizes = null;
		try {
			getSupportedPreviewSizes = parameters.getClass()
					.getMethod("getSupportedPreviewSizes", new Class[] {});
			if (getSupportedPreviewSizes != null) {
				@SuppressWarnings("unchecked")
				List<Size> frameSizes = (List<Size>) getSupportedPreviewSizes
						.invoke(parameters, (Object[]) null);
				if (frameSizes != null) {
					Size opSize = getOptimalEqualPreviewSize(frameSizes, 320, 240);
					if (opSize!=null) {
					    VideoChatSettings.setWidth(opSize.width);
	                    VideoChatSettings.setHeight(opSize.height);
					}
				}
			}
		} catch (Exception e) {
		}
	}
	
	/**
	 * 获取最佳预览大小
	 * @param sizes
	 * @param screen_width
	 * @param screen_height
	 * @return
	 */
	private Size getOptimalEqualPreviewSize(List<Size> sizes, int screen_width, int screen_height) {
		Size optimalSize = null;
		int MaxLength = Math.min(screen_width, screen_height);
		// Try to find an size match aspect ratio and size
		for (Size size : sizes) {
			if (size.width == size.height && size.width <= MaxLength) {
				if (optimalSize == null || optimalSize.width < size.width) {
					optimalSize = size;
				}
			}
		}
		// Cannot find the one match the aspect ratio, ignore the requirement
		if (optimalSize == null) {
			optimalSize = getOptimalPreviewSize(sizes,
					VideoChatSettings.getWidth(), VideoChatSettings.getHeight());
		}

		return optimalSize;
	}
	
	/**
	 * 获取最佳预览大小
	 * @param sizes
	 * @param w
	 * @param h
	 * @return
	 */
	private Size getOptimalPreviewSize(List<Size> sizes, int w, int h) {
		final double ASPECT_TOLERANCE = 0.05;
		double targetRatio = (double) w / h;
		if (sizes == null)
			return null;
		Size optimalSize = null;
		double minDiff = Double.MAX_VALUE;
		int targetHeight = h;
		// Try to find an size match aspect ratio and size
		for (Size size : sizes) {
			double ratio = (double) size.width / size.height;
			if (Math.abs(ratio - targetRatio) > ASPECT_TOLERANCE)
				continue;
			if (Math.abs(size.height - targetHeight) < minDiff) {
				optimalSize = size;
				minDiff = Math.abs(size.height - targetHeight);
			}
		} // Cannot find the one match the aspect ratio, ignore the requirement
		if (optimalSize == null) {
			minDiff = Double.MAX_VALUE;
			for (Size size : sizes) {
				if (Math.abs(size.height - targetHeight) < minDiff) {
					optimalSize = size;
					minDiff = Math.abs(size.height - targetHeight);
				}
			}
		}
		return optimalSize;
	}
	
	
	/**
	 * 获取并开启前置摄像头(由getFrontCarmera调用)
	 * @return
	 */
	private Camera openFrontFacingCamera() {
		Camera c = null;
		this.CameraId = 0;
		try{
			Class cameraClass = Class.forName("android.hardware.Camera");
			Object cameraInfo = null;
			Field field_facing = null;
			
			if (this.NUM_CAMERA == 0) {
				this.NUM_CAMERA = GetNumberOfCamera();
			}
			
			if (this.NUM_CAMERA > 1) {
				Class cameraInfoClass = Class.forName("android.hardware.Camera$CameraInfo");
				
				if (cameraInfoClass != null) {
		          cameraInfo = cameraInfoClass.newInstance();
		        }

		        if (cameraInfo != null) {
		          field_facing = cameraInfo.getClass().getField("facing");
		        }

		        Method getCameraInfo = cameraClass.getMethod("getCameraInfo", new Class[] { 
		          Integer.TYPE, cameraInfoClass });

		        if ((getCameraInfo != null) && (cameraInfoClass != null) && (field_facing != null))
		          for (int camIdx = 0; camIdx < this.NUM_CAMERA; camIdx++) {
		            getCameraInfo.invoke(null, new Object[] { Integer.valueOf(camIdx), cameraInfo });
		            int facing = field_facing.getInt(cameraInfo);
		            if (facing == 1)
		              try {
		                Method cameraOpen = cameraClass.getMethod("open", new Class[] { Integer.TYPE });
		                if (cameraOpen != null) {
		                  c = (Camera)cameraOpen.invoke(null, new Object[] { Integer.valueOf(camIdx) });
		                  this.CameraId = camIdx;
		                }
		              } catch (RuntimeException e) {
		                Log.e(TAG, "openFrontFacingCamera", e);
		              }
		          }
		      }
		    }
		    catch (ClassNotFoundException e)
		    {
		      Log.e(TAG, "openFrontFacingCamera", e);
		    } catch (NoSuchMethodException e) {
		      Log.e(TAG, "openFrontFacingCamera", e);
		    } catch (NoSuchFieldException e) {
		      Log.e(TAG, "openFrontFacingCamera", e);
		    } catch (IllegalAccessException e) {
		      Log.e(TAG, "openFrontFacingCamera", e);
		    } catch (InvocationTargetException e) {
		      Log.e(TAG, "openFrontFacingCamera", e);
		    } catch (InstantiationException e) {
		      Log.e(TAG, "openFrontFacingCamera", e);
		    } catch (SecurityException e) {
		      Log.e(TAG, "openFrontFacingCamera", e);
		    } catch (Exception e) {
		      Log.e(TAG, "openFrontFacingCamera", e);
		    }
		    return c;
	}


	/**
	 * 获取并开启前置摄像头
	 * @return
	 */
	private boolean getFrontCamera() {
	    if (fitSdkVersion()) {//开前置摄像头需要sdk10以上才支持
	    	this.camera = openFrontFacingCamera();
	    }
	    if(this.camera == null){
	        Log.d("VcCamera", "openFrontCamera camera == null");
	        isVcOpened = false;
	        return false;
	    }
	    //如果打开了前置相机，那肯定有两个
	    /*
	    if(this.NUM_CAMERA == 0){
	        this.NUM_CAMERA = 2;
	    }*/
	    this.CUR_CAMERA = 1;
	    //isCameraOpened = true;
	    Log.d(TAG, "openFrontCamera success");
	    return true;
	}
	
	/**
	 * 开启后置摄像头
	 * @return
	 */
	private boolean getBackCamera() {
		try {
			this.camera = Camera.open();
		} catch (Exception e) {
			isVcOpened = false;
			if(this.camera != null){
		        this.camera.release();
		    }			
			Log.d(TAG, "openBackCamera exception");
			return false;
		}		
		this.CUR_CAMERA = 2;
		//isCameraOpened = true;
		this.CameraId = 0;
		Log.d(TAG, "openBackCamera success");
		return true;
	}
	
	/**
	 * 切换摄像头，外部调用
	 * @return
	 */
	public synchronized boolean switchCamera() {
		if (NUM_CAMERA < 2 || camera == null){
			return false;
		}
		
		Log.d(TAG, "switchCamera: " + ((CUR_CAMERA == FRONT_CAMERA) ? "FRONT_CAMERA" : "BACK_CAMERA"));
		
		closeCamera();
		
		switch(CUR_CAMERA) {
			case FRONT_CAMERA:
				if(getBackCamera()) {
					return true;
				}
				break;
			case BACK_CAMERA:
				if(getFrontCamera()) {
					return true;
				}
				break;
		}
		
		return false;
	}
	  
	/**
	 * 获取摄像头个数
	 * @return
	 */
	public int GetNumberOfCamera() {
		int CameraCnt = 1;
		try {
			Class<?> cameraClass = Class.forName("android.hardware.Camera");
			Method getNumOfCamera = cameraClass.getMethod("getNumberOfCameras", new Class[] {});
			Object result = getNumOfCamera.invoke(null, (Object[]) null);
			CameraCnt = Integer.parseInt(result.toString());
		} catch (Exception e) {
			e.printStackTrace();
		}
		Log.d(TAG, "GetNumberOfCamera...CameraCnt="+CameraCnt);
		return CameraCnt;
	}
	
	/**
	 * 获取摄像机
	 * @return
	 */
	public Camera getCamera() {
		return this.camera;
	}
	
	/**
	 * 必须大于等于10的sdk版本
	 * @return
	 */
	private boolean fitSdkVersion(){
	    return this.SDK_VERSION >= 10;
	}
	

	
	public boolean isCameraOpened() {
		return isVcOpened;
	}
	
	
	/**
	 * 镜头属性
	 * @author Administrator
	 *
	 */
	private static class CameraInformation {
		int facing;//面朝向
		int orientation;//方向，横还是竖
		int rotation;//旋转角度
		
		@Override
		public String toString() {
			return "facing="+facing+",orientation="+orientation+",rotation="+rotation;
		}
	}
	
	/**
	 * 视频属性
	 * @author Administrator
	 *
	 */
	public static class VideoChatSettings {

		private static int initial_width = 320;//320;//192;//144;//176;
		private static int initial_height = 240;//240;//240;//192;//144;
	    private static int width = initial_width;
	    private static int height = initial_height;
		private static int format = PixelFormat.UNKNOWN;
		private static int nInFPS = 10;
		
	    public static int getnInFPS() {
			return nInFPS;
		}

		public static void setnInFPS(int nInFPS) {
			VideoChatSettings.nInFPS = nInFPS;
		}
		
	    
	    public static int getWidth() {
	        return width;
	    }
	    
	    public static int getHeight() {
	        return height;
	    }
	    
	    public static void setWidth(int w) {
	    	width = w;
	    }
	    
	    public static void setHeight(int h) {
	    	height = h;
	    }
	    
	    public static void reset()
	    {
	    	width = initial_width;
	    	height = initial_height;
	    }
	    
	    public static int getFormat() {
	    	return format;
	    }
	    
	    public static void setFormat(int f) {
	    	format = f;
	    }
	    
	    public static String getVal(){
	    	StringBuffer f = new StringBuffer();
	    	f.append("initial_width=").append(initial_width).append(",");
	    	f.append("initial_height=").append(initial_height).append(",");
	    	f.append("width=").append(width).append(",");
	    	f.append("height=").append(height).append(",");
	    	f.append("format=").append(format).append(",");
	    	f.append("nInFPS=").append(nInFPS);
	    	return f.toString();
	    }
	}
	
}
